package com.example.demo.service.impl;

import com.example.demo.Exception.MyExcption;
import com.example.demo.Exception.NOException;
import com.example.demo.dao.LearnDao;
import com.example.demo.pojo.Learn;
import com.example.demo.service.LearnService;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created by dingkl on 2017/7/13.
 */
@Service
public class LearnServiceImpl implements LearnService{

    @Autowired
    LearnDao learnDao;

    @Override
    public Learn getById(Learn learn) {
        return learnDao.getById(learn);
    }

    @Override
    public List<Learn> getList(int page,int pageSize) {
        PageHelper.startPage(page,pageSize);
        return learnDao.getList();
    }

    @Override
    @Transactional(timeout = 30,rollbackFor = MyExcption.class,noRollbackFor = {NOException.class})
    public int add(Learn learn) throws Exception{
        int flag = learnDao.add(learn);
        if (2001==learn.getId()){
            throw new MyExcption("123");
        } else if (2002==learn.getId()){
            throw new NOException("no123");
        }
        return flag;
    }
}
